package it.gat.mobile;

import it.gat.controller.ContrattoController;
import it.gat.controller.TipologiaDispositivoController;
import it.gat.controller.utils.GatSession;
import it.gat.model.Cliente;
import it.gat.model.Contratto;
import it.gat.model.TipologiaDispositivo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.os.Build;

public class ContrattoActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contratto);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contratto, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		
		private TextView txtDataInizio;
		private TextView txtDataFine;
		private TextView txtOrePreviste;
		private TextView txtOreUsufruite;
		private TextView txtOreRimanenti;

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_contratto,
					container, false);
			
			txtDataInizio = (TextView)rootView.findViewById(R.id.txtDataInizio);
			txtDataFine = (TextView)rootView.findViewById(R.id.txtDataFine);
			txtOrePreviste = (TextView)rootView.findViewById(R.id.txtOrePreviste);
			txtOreUsufruite = (TextView)rootView.findViewById(R.id.txtOreUsufruite);
			txtOreRimanenti = (TextView)rootView.findViewById(R.id.txtOreRimanenti);
			
			new ContrattoTask(rootView.getContext()).execute((Void)null);
			
			return rootView;
		}
		
		public class ContrattoTask extends AsyncTask<Void, Void, Boolean> {

			private Context context;
			private Contratto contratto;

			private ContrattoTask(Context context) {
				this.context = context;
			}

			@Override
			protected Boolean doInBackground(Void... params) {


				ContrattoController cc = new ContrattoController();
				contratto = cc.getContrattoAttivo((Cliente)GatSession.getInstance().getUtenteLoggato());

				return true;

			}

			@Override
			protected void onPostExecute(final Boolean success) {
				if (success) {
					SimpleDateFormat format =new SimpleDateFormat("dd/MM/yyyy");
					txtDataInizio.setText(format.format(contratto.getDataInizio()));
					txtDataFine.setText(format.format(contratto.getDataFine()));
					txtOrePreviste.setText(Integer.toString(contratto.getOrePreviste()));
					txtOreUsufruite.setText(Integer.toString(contratto.getOreUsufruite()));
					txtOreRimanenti.setText(Integer.toString(contratto.getOrePreviste()-contratto.getOreUsufruite()));
				}
			}
		}
	}

}
